#include "DebugConsole.h"

#include <Windows.h>
#include <iostream>

#define _DEBUG

HANDLE __hStdOut = NULL;

void BuildDebugConsole()
{
#ifdef _DEBUG
	AllocConsole();
	SetConsoleTitle("Debug");
	__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

	COORD co = {80,25};
	SetConsoleScreenBufferSize(__hStdOut, co);
#endif
}

void ReleaseDebugConsole()
{
#ifdef _DEBUG
	FreeConsole();
	__hStdOut = NULL;
#endif
}

int ConsolePrint(char *fmt, ...)
{
#ifdef _DEBUG
	char s[300];
	va_list argptr;
	int cnt;

	va_start(argptr, fmt);
	cnt = vsprintf(s, fmt, argptr);
	va_end(argptr);

	DWORD cCharsWritten;

	if(__hStdOut)
		WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten, NULL);

	return(cnt);
#else
	return 0;
#endif
}